import { defineStore } from 'pinia'
import * as apis from '@/api';


interface State {
  netTk: any; // 非现金售票占比和金额
  salesOper: any; // 定制售票数和其他
  station: any; // 15个乘车站汽车票日营收
  sellTk: any; // 各个站的窗口和网售汽车票的售票数
  popularStop: any; // 热门站点top
  salesOrderTk: any; // 定制售票数和其他
}

export const useHomeStore = defineStore('home', {
  state: (): State => {
    return {
      netTk: {}, // 非现金售票占比和金额
      salesOper: {},
      station: [], // 15个乘车站汽车票日营收
      sellTk: [], // 各个站的窗口和网售汽车票的售票数
      popularStop: [], // 热门站点top
      salesOrderTk: [], // 定制售票数和其他
    }
  },
  getters: {

  },
  actions: {
    // 非现金售票占比和金额
    async greenNetTkIncome() {
      const res:any = await apis.GreenNetTkIncome()
      const { code, data: dataObj } = res.data
      if (+code !== 0) return
      const [sourceData] = dataObj;
      const chartData = {
        legendData: ['携程', '隆运', '自营'],
        seriesData: [
          {
            name: '携程',
            value: +sourceData.xC_TotalTkPrice,
          },
          {
            name: '隆运',
            value: +sourceData.lY_TotalTkPrice,
          },
          {
            name: '自营',
            value: +sourceData.zY_TotalTkPrice,
          },
        ],
        ...sourceData,
      }
      this.netTk = chartData;
    },
    // 15个乘车站汽车票日营收
    async greenStationIncome() {
      const res:any = await apis.GreenStationIncome()
      const { code, data: dataObj } = res.data
      if (+code !== 0) return
      const chartData = dataObj.reduce((prev:any, next: any) => {
        prev.yAxisData = [...prev.yAxisData, next.carryStationName];
        prev.seriesData = [...prev.seriesData, +next.totalTkPrice];
        return prev
      }, {
        yAxisData: [],
        seriesData: [],
      })
      chartData.yAxisData.reverse();
      chartData.seriesData.reverse();
      this.station = chartData;
    },
    // 售票总数、检票总数、售票金额
    async greenSellOperIncome() {
      const res:any = await apis.GreenSellOperIncome()
      console.log(`output->res`,res)
      const { code, data: dataObj } = res.data
      console.log(`output->dataObj`,dataObj)
      if (+code !== 0) return
      this.salesOper = dataObj;
      return dataObj;
    },
    // 各个站的窗口和网售汽车票的售票数
    async greenSellTk() {
      const res:any = await apis.GreenSellTk()
      const { code, data: dataObj } = res.data
      if (+code !== 0) return
      const chartData = dataObj.reduce((prev:any, next: any) => {
        prev.xAxisData = [...prev.xAxisData, next.carryStationName];
        prev.windowSeriesData = [...prev.windowSeriesData, +next.stationTotalTkCount];
        prev.netSeriesData = [...prev.netSeriesData, +next.netTotalTkCount];
        return prev
      }, {
        xAxisData: [],
        windowSeriesData: [],
        netSeriesData: [],
      })
      this.sellTk = chartData;
    },
    // 热门站点top
    async greenPopularStop() {
      const res:any = await apis.GreenPopularStop()
      const { code, data: dataObj } = res.data
      if (+code !== 0) return
      const chartData = dataObj.map((item: any) => {
        return {
          name: item.stopName,
          value: +item.sellCount,
        }
      })
      this.popularStop = chartData;
    },
    async greenSalesOrderTk() {
      const res:any = await apis.GreenSalesOrderTk()
      const { code, data: dataObj } = res.data
      if (+code !== 0) return
      const [sourceData] = dataObj;
      const chartData = [
        {
          value: +sourceData.dzMoney,
          name: '定制售票数',
        },
        {
          value: +sourceData.totalMoney - +sourceData.dzMoney,
          name: '其他',
        },
      ]
      this.salesOrderTk = chartData;
    },
  },
})